// pages/homework/homework.js
const App = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    routeQuery: {}, //url参数
    reviewInfo: {}, //作业详情
    afterClassScore: {
      name: '评分',
      value: 0,
      type: 'afterClassScore'
    }, //专业
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options) {
      console.log(options)
      this.setData({
        routeQuery: options
      })
      this.getReviewInfo(options.id, options.studentUserId)
    }
  },
  async getReviewInfo(timetableDetailedId, studentUserId) {
    // 团课-作业点评详情
    let data = await App.$axios.request({
      url: `/teacher/big/reviewInfo`,
      method: 'GET',
      loading: true,
      data: {
        timetableDetailedId: timetableDetailedId,
        studentUserId: studentUserId
      }
    })
    if (data) {
      this.setData({
        reviewInfo: data
      })
    }
  },
  changeStar(e) {
    // 修改星星
    if (this.data.reviewInfo.reviewStatus == 3) return
    if (this.data.reviewInfo.reviewFinished) return
    let data = e.detail
    let value = [data.type] + '.value'
    this.setData({
      [value]: data.value + 1
    })
  },
  textChange(e) {
    // 课后建议修改
    let value = e.detail.value
    this.setData({
      'reviewInfo.afterClassAssessment': value
    })
  },
  async submit(e) {
    // 提交点评
    if (this.data.reviewInfo.reviewFinished) {
      wx.showToast({
        title: '作业已评价',
        icon: 'none',
        duration: 2000
      })
      return
    }

    if (!this.data.afterClassScore.value) {
      wx.showToast({
        title: '评分不能为0',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.reviewInfo.afterClassAssessment) {
      wx.showToast({
        title: '课后建议不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let data = await App.$axios.request({
      url: `/teacher/big/review`,
      method: 'PUT',
      loading: true,
      data: {
        timetableDetailedId: this.data.routeQuery.id,
        studentUserId: this.data.routeQuery.studentUserId,
        afterClassScore: this.data.afterClassScore.value,
        afterClassAssessment: this.data.reviewInfo.afterClassAssessment
      }
    })
    if (data) {
      wx.showToast({
        title: '提交成功',
      })
    }
    if (data && e.currentTarget.dataset.type == 'jump') {
      // wx.redirectTo({
      //   url: `/pages/homework/homework?id=${data.timetableDetailedId}&studentUserId=${data.studentUserId}&courseName=${this.data.courseDetail.curriculumName}`,
      // })
      let options = {
        courseName: this.data.courseDetail.curriculumName,
        id: data.timetableDetailedId,
        studentUserId: data.studentUserId
      }
      this.setData({
        routeQuery: options,
        reviewInfo: data
      })
      // this.getReviewInfo(options.id, options.studentUserId)
    } else {
      let options = {
        id: this.data.routeQuery.timetableDetailedId,
        studentUserId: this.data.routeQuery.studentUserId
      }
      this.setData({
        'reviewInfo.reviewStatus': 3,
      })
      // this.getReviewInfo(options.id, options.studentUserId)
    }
  }
})